define(['jquery', 'bootstrap', 'backend', 'table', 'form','jstree'], function ($, undefined, Backend, Table, Form,undefined) {


    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'information/oauser/index' + location.search
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                singleSelect:true,
                fixedColumns: true,
                fixedRightNumber: 1,
                showExport: false,
                showToggle:false,
                showFullscreen:true,
                striped:true,
                remember: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'no', title:'编码', operate: 'LIKE',visible:false},
                        {field: 'user_name', title: '用户名', operate: 'LIKE'},
                        {field: 'dept_name', title: '部门', operate: 'LIKE'},
                        {field: 'user_id', title:'用户ID', operate: false,visible:false},
                        {field: 'dept_id', title: '部门ID', operate: false,visible:false},
                    ]
                ]
            });

            $(".search>input").eq(0).addClass('searchtyle')
            
            // 为表格绑定事件
            Table.api.bindevent(table);
            $('#choose_id').on('click',function(){
            	let ids =  Controller.api.selectedids(table);
            	if(ids==''){
            		Toastr.error('请选择领料人');
            		return ;
            	}
            	if($("#backoa").val()!=1){
    			  Toastr.error('请先开启回写OA配置');
    			  return
    		    }
            	
            	var row = Table.api.getrowbyid(table, ids[0]);//获取选中列的值
                Fast.api.close(row);
            })
            
            //加载左侧树
            Controller.api.tree();
        },
        recyclebin: function () {
            // 为表格绑定事件
            Controller.api.bindevent();
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            //获取选中的行
            selectedids: function (table, current) {
                var options = table.bootstrapTable('getOptions');
                //如果有设置翻页记忆模式
                if (!current && options.maintainSelected) {
                    return options.selectedIds;
                }
                return $.map(table.bootstrapTable('getSelections'), function (row) {
                    return row[options.pk];
                });
            },

            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            rendertree: function (content) {
                $("#treeview")
                    .on('redraw.jstree', function (e) {
                        $(".layer-footer").attr("domrefresh", Math.random());
                    }).on('changed.jstree', function (e, data) {
                    let node_id = data.node.id;
                    let node_parent = data.node.parent;
                    let node_parents = data.node.parents;
                    let parents_length = node_parents.length;
                    let text = data.node.text;
                    
                    if(node_parent!='#'){
                    	var text_arr = text.split(' ');
                    	$("[name='dept_name']").val(text_arr[1])
                    }
                    
                    $("[type='submit']").trigger('click')
                }).jstree({
                    "plugins": [ "themes",'types'],
                    "core": {
                        'check_callback': true,
                        "data": content
                    }
                });
            },
            tree:function (){
                //let warehouse_id =  $("[name='warehouse_id']").val();
                $.ajax({
                    url: "information/oauser/getTreeData",
                    type: 'post',
                    dataType: 'json',
                    //data: {warehouse_id: warehouse_id},
                    success: function (ret) {
                        if (ret.hasOwnProperty("code")) {
                            var data = ret.hasOwnProperty("data") && ret.data != "" ? ret.data : "";
                            if (ret.code === 1) {
                                //销毁已有的节点树
                                $("#treeview").jstree("destroy");
                                Controller.api.rendertree(data);
                            } else {
                                Backend.api.toastr.error(ret.msg);
                            }
                        }
                    }, error: function (e) {
                        Backend.api.toastr.error(e.message);
                    }
                })
            }
        }
    };
    return Controller;
});
